<?php
namespace Home\Controller;

class InvestController extends \Home\Controller\BaseController
{

	
	/**
	 * 查询投资记录
	 */
	public function investListBySelf()
	{
		$param = I('params');

		if(!$param) $this->ajaxReturn(array('error' => 1, 'info' => '参数错误'));
		
		// 构造查询条件		
		$where['user_id'] = $this->user['userId'];
		if(I('data.id', '', '/^\d+$/', $param)) $where['id'] = $param['id'];
		$where['status'] = array('gt', 0);
		
		$list = D('Invest')->searchInvest($where);
		$this->ajaxReturn($this->result($list)); 
	}
	
	/**
	 * 查询投资记录
	 */
	public function investList()
	{
		$param = I('params');
		if(!$param) $this->ajaxReturn(array('error' => 1, 'info' => '参数错误'));
		// 返回字段
		$field = 'id,user_id,good_info,amount,create_time,order_no';

		// 构造查询条件
		$where = array();

		$where['status'] = $param['status'];
		$where['user_id']=$this->user['userId'];
		$where['type']=$param['type'];
		$option['page'] = I('data.page', '', '/^\d+$/', $param) ? $param['page'] : 1;
		$option['size'] = I('data.size', '', '/^\d+$/', $param) ? $param['size'] : 5;
		
		$list = D('Invest')->searchInvest($where, $option, 'create_time desc', $field);
		$this->ajaxReturn($this->result($list));
	}
	
	/**
	 * 下单
	 * @author 刘新
	 */
	public function addOrder()
	{
		$param = I('params');
		$data['user_id'] = $this->user['userId'];
		$auto = $param['auto'];//是否自动下单 1
		$type = $param['type'];
		$data['type'] = $type;//1 普通商品 2预约商品 3 定制商品
		$data['receipt_id'] = $param['receipt_id'];
		if($auto == 1 || $type != 1){
			$good_info = $param['good_info'];
			$data['good_info'] = json_encode($good_info);
			$data['amount'] = $param['amount'];
			if($type == 2){
				$data['delivery'] = $param['delivery'];//预计发货时间
			}elseif($type == 3){
				$data['production_time'] = $param['production_time'];//生产周期
			}
		}else{
			//购物车id
			$card_ids = explode(",",$param['card_ids']);
			$where['id'] = array("in",$card_ids);
			$cardinfo = M("Card")->where($where)->select();
			$good_info = array();
			$amount = 0;
			foreach($cardinfo as $key=>$val){
				if($val['status'] == 1){
					$good_info[$key]["good_id"] = $val['good_id'];
					$good_info[$key]["count"] = $val['count'];
					$good_info[$key]["price"] = $val['price'];
					$good_info[$key]["sku"] = $val['sku'];
					$amount += $val['price']*$val['count'];
				}
			}
			$data['good_info'] = json_encode($good_info);
			$data['amount'] = $amount;
			//M("Card")->where($where)->delete();
		}
		$res = D('Invest')->addOrder($data, $this->user['userId']);
		$this->ajaxReturn($res);
	}
	
	/**
	 * 赎回
	 * @author 刘新
	 */
	public function redeem()
	{
		$param = I('params');
		$data['invest_id'] = I('data.invest_id', '', '/^[1-9]\d*$/', $param);
		$data['count'] = I('data.count', '', '/^\d+$/', $param);
		$data['user_id'] = I('data.user_id', '', '/^\d+$/', $param);
		$res = D('Redeem')->addRedeem($data);
		$this->ajaxReturn($res);
	}
	
	/**
	 * 赎回时查询是否违约及违约金
	 */
	public function isDefaults()
	{
		$param = I('params');
		$investId = I('data.invest_id', '', '/^[1-9]\d*$/', $param);
		$count = I('data.count', '', '/^\d+$/', $param);
		$res = D('Invest')->defaultsInfo($count, $investId);
		$this->ajaxReturn($res);
	}
	
	/**
	 * 投资收益
	 * @author 刘新
	 */
	public function profit()
	{
		$param = I('param');
	}
	//插入购物车
	public function setcard(){
		$param = I("params");
		$data['good_id'] = $param['good_id'];
		$data['count'] = $param['count'];
		$data['price'] = $param['price'];
		$data['sku'] = $param['sku_id'];
		$data['status'] = '1';//1 正常 2 失效
		$data['user_id'] = $this->user['userId'];
		$data['create_time'] = time();
		$id = $param['id'];
		if(empty($id)){
			$info['id'] = M("Card")->add($data);
			$this->ajaxReturn(array('error' => 0, 'info' => $info));
		}else{
			M("Card")->where(array("id"=>$id))->save($data);
			$this->ajaxReturn(array('error' => 0, 'info' =>"修改成功"));
		}
	}
	//删除购物车的商品
	public function deletecard(){
		$param = I("params");
		$id = $param['id'];
		$id = M("Card")->where(array("id"=>$id))->delete();
		$this->ajaxReturn(array('error' => 0, 'info' =>"删除成功"));
	}
	//购物车列表
	public function cardlist(){
		$data['mt_card.user_id'] = $this->user['userId'];
		$res = M("Card")
			->join("left join mt_good on mt_card.good_id=mt_good.id")
			->join("left join mt_good_price on mt_card.good_id=mt_good_price.good_id AND mt_card.`sku`=mt_good_price.`id`")
			->where($data)
			->field(array("mt_card.*","mt_good.img_path","mt_good.title","mt_good.original_price","mt_good_price.category_id"))
			->select();
		$return = array();
		foreach($res as $key=>$val){
			$return[$key] = $val;
			$return[$key]['img_path'] = explode(",",$val['img_path'])[0];
			$return[$key]['min_price'] = floatval($val['price']);
			$return[$key]['count'] = intval($val['count']);
			$return[$key]['sku_id'] = $val['sku'];
			$return[$key]['original_price'] = floatval($val['original_price']);
			$card_ids = explode(",",$val['category_id']);
			$where['id'] = array("in",$card_ids);
			$cateinfo = M("GoodCategory")->where($where)->select();
			$catelist = "";
			if(is_array($cateinfo)){
				foreach($cateinfo as $ks=>$vs){
					$catelist .= $vs['name'].":".$vs['value'].",";
				}
			}
			$return[$key]['sku'] = substr($catelist,0,-1);;
		}
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}

}